package com.xu.crawler.pipeline;

import com.xu.crawler.dao.NewsDao;
import com.xu.crawler.dao.entity.NewsDO;
import com.xu.crawler.util.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.model.BaseDO;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 数据持久化到mongo中
 * Created by clear on 2018/7/11.
 */
@Component
public class MongoPipeline implements Pipeline {
    private final static Logger log = LoggerFactory.getLogger(MongoPipeline.class);
    @Resource
    private NewsDao newsDao;

    private AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void process(BaseDO baseDO, Task task) {
        if (newsDao == null) {
            newsDao = SpringUtils.getBean(NewsDao.class);
        }
        newsDao.saveNews((NewsDO) baseDO);
    }


}
